package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * dao层
 * 封装sql语句
 * 调用jdbc
 * 以后：
 * 能够在前端做的事情不能给java后台
 * 能够在mysql写的事情不能给Java
 */
public class UserImpl implements UserDao {
    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from user where username=? and password=?";
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(JDBC.getMD5Str(userModel.getPassword()));
//        list.add(userModel.getId());
        ResultSet query = JDBC.query(sql, list);
        UserModel model = null;
        try {
            while (query.next()) {
                model = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int add(UserModel userModel) {
        String sql = "insert into user(username,password)values(?,?)";
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(JDBC.getMD5Str(userModel.getPassword()));
        return JDBC.update(sql, list);
    }

    @Override
    public int update(UserModel userModel) {

        return JDBC.update("", null);
    }

    @Override
    public int delete(UserModel userModel) {

        return JDBC.update("", null);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        String sql = "select * from user  ";
        ResultSet query = JDBC.query(sql, null);
        List<UserModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                UserModel model = new UserModel();
                model.setId(query.getInt("id"));
//                密码需要加密，加密后存入数据库，所以不能查询出密码
                model.setUsername(query.getString("username"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public UserModel findById(UserModel userModel) {
        String sql = "select * from user  ";
        ResultSet query = JDBC.query(sql, null);
        UserModel model = null;
        try {
            while (query.next()) {
                model = new UserModel();
                model.setId(query.getInt("id"));
//                密码需要加密，加密后存入数据库，所以不能查询出密码
                model.setUsername(query.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int getCount(UserModel userModel) {
        String sql = "select count(*) count from user ";
        ResultSet query = JDBC.query(sql, null);
        try {

            while (query.next()) {
                return query.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
